﻿using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HotFixPackager.code
{
    class FileMD5Helper
    {
        #region 公共方法

        /// <summary>
        /// 计算文件的md5值，返回小写格式
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string generateFileMD5Lower(string url)
        {
            if (File.Exists(url) == false)
                return string.Empty;

            byte[] fileByte = File.ReadAllBytes(url);

            if (fileByte == null)
                return string.Empty;

            byte[] hashByte = new MD5CryptoServiceProvider().ComputeHash(fileByte);

            return byteArrayToString(hashByte);
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 输出数据的十六进制字符串
        /// </summary>
        /// <param name="arrInput"></param>
        /// <returns></returns>
        private static string byteArrayToString(byte[] arrInput)
        {
            StringBuilder sOutput = new StringBuilder(arrInput.Length);

            for (int i = 0; i < arrInput.Length; i++)
            {
                sOutput.Append(arrInput[i].ToString("x2"));
            }
            return sOutput.ToString();
        }

        #endregion
    }
}
